package com.sinosoft.moniter.servlet;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.sinosoft.moniter.services.MonitorService;
import com.sinosoft.moniter.services.impl.MonitorServiceImpl;
import com.sinosoft.moniter.utils.Config;
import com.sinosoft.moniter.utils.StringUtils;

public class MonitorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String path = request.getParameter("path")+"";
		if("".equals(path)|| "null".equals(path)){
			path="/";
		}
		if(path.indexOf("/..")!=-1){
			path=StringUtils.subStr(path, "/..");
		}
		if(path.indexOf("/.")!=-1){
			path=StringUtils.subStr(path, "/.");
		}
		String common = request.getParameter("common");
		if ("on".equals(common)) {
			if (!Config.commonPath.containsKey(path)) {
				Config.commonPath.put(path, path);
			}
		}
//		System.out.println("path:" + path);
//		System.out.println("common:" + common);
		MonitorService ms = new MonitorServiceImpl();
		Vector<LsEntry> v = ms.service(path);
		request.getSession().setAttribute("v", v);
		request.getSession().setAttribute("vv_path", path);
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
}
